use Flap qw/-m -s -w/;

sub{
    my $os = shift;
    my $proj = $os->project;
    my $m = $proj->Mailer->new(@_)->filter;
    
    my $H = $m->sendmail_handle;
    print $H "$_: ".$m->mime_encode($m->{header}{$_})."\n" for sort keys %{$m->{header}};
    $m->jis_encode(\(my $body = ${$m->{body}}));
    my $boundary;
    if($m->{attachment}){
        while(1){
            $boundary = '----=_NextPart_'.rand;
            last if index($body, $boundary) < 0;
        }
        print $H qq{Content-Type: multipart/mixed; boundary="$boundary"\n\n};
        print $H "This is a multi-part message in MIME format.\n\n";
        print $H "--$boundary\n";
    }
    print $H qq{Content-Type: text/plain; charset="iso-2022-jp"\n};
    print $H "Content-Transfer-Encoding: 7bit\n\n";
    print $H $body;
    if($m->{attachment}){
        print $H "\n";
        require MIME::Base64;
        foreach my $path (sort keys %{$m->{attachment}}){
            my $att = $m->{attachment}{$path};
            my $fn = $m->mime_encode($att->[0]);
            print $H "\n--$boundary\n";
            print $H "Content-Type: $att->[1]\n";
            print $H qq{Content-Disposition: attachment; filename="$fn"\n};
            print $H "Content-Transfer-Encoding: base64\n\n";
            $os->cat(my $buf, '<', $path) || die "$!($path)";
            print $H MIME::Base64::encode_base64($buf);
        }
        print $H "\n--$boundary--\n";
    }
    close($H);
};
